package org.ediba.siri.web.rest;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ediba.siri.core.Filter;
import org.ediba.siri.core.image.domain.Image;
import org.ediba.siri.core.image.service.ImageService;
import org.ediba.siri.core.model.domain.Model;
import org.ediba.siri.core.model.domain.ModelModel;
import org.ediba.siri.core.model.domain.ModelSchool;
import org.ediba.siri.core.model.service.ModelService;

public class ModelController extends Controller {

	private ModelService modelService;
	public void setModelService(ModelService modelService) {this.modelService = modelService;}
	
	private ImageService imageService;
	public void setImageService(ImageService imageService) {this.imageService = imageService;}
	
	public Map<String,Object> getHandler(HttpServletRequest request, HttpServletResponse response, ModelCommand modelCommand){
		
		List<Model> models = new ArrayList<Model>();
		Integer total = 0;
		
		if (modelCommand.getId() != null){
			Map<String,Object> _result = new HashMap<String,Object>(4);
			_result.put("data", this.modelService.retrieve(modelCommand.getId()));
			_result.put("total", 1);
			_result.put("success", true);
			_result.put("message", "OK");
			return _result;
		}
		
		if (modelCommand.getQuery() != null){
			models = this.modelService.retrieve(modelCommand.getQuery(),modelCommand.getStart(), modelCommand.getLimit());
			total = this.modelService.retrieveCount();
		}
		else if (modelCommand.getIdImage() != null){
			//models = new ArrayList<Model>(1);
			models = new ArrayList<Model>(this.imageService.retrieve(modelCommand.getIdImage()).getModels());
			total = models.size();
		}
		else if (modelCommand.getIdsImage() != null){
			models = new ArrayList<Model>(this.imageService.retrieve(modelCommand.getIdsImage()[0]).getModels());
			total = models.size();
		}
		else if (modelCommand.getIdsModel()!=null){
			List<ModelModel> modelModels = null;
			Integer idModel = modelCommand.getIdsModel()[0];
			modelModels = new ArrayList<ModelModel>(this.modelService.retrieve(idModel).getModelModelsForIdModelA());
			for (ModelModel modelModel : modelModels) {
				models.add(modelModel.getModelByIdModelB());
			}
		}
		else{
			
			Map<Filter,Object> filters = new HashMap<Filter, Object>(8);
			if (modelCommand.getFirstName()!=null&&!modelCommand.getFirstName().isEmpty())
				filters.put(Filter.FIRSTNAME, modelCommand.getFirstName());
			if (modelCommand.getLastName()!=null&&!modelCommand.getLastName().isEmpty())
				filters.put(Filter.LASTNAME, modelCommand.getLastName());
			if (modelCommand.getFeature()!=null&&!modelCommand.getFeature().isEmpty())
				filters.put(Filter.MODELFEATURE, modelCommand.getFeature());
			if (modelCommand.getGender()!= null)
				filters.put(Filter.GENDER, modelCommand.getGender());
			if (modelCommand.getIdModelSchool()!=null)
				filters.put(Filter.MODELSCHOOL, modelCommand.getIdModelSchool());
			if (modelCommand.getTurn() != null)
				filters.put(Filter.TURN,modelCommand.getTurn());
			if (modelCommand.getAgeEnd() != null){
				Calendar birthdateMin = Calendar.getInstance();
				birthdateMin.setTime(new Date());
				birthdateMin.add(Calendar.YEAR, -modelCommand.getAgeEnd());
				filters.put(Filter.BIRTHDATE_MIN, birthdateMin.getTime());
			}
			if (modelCommand.getAgeStart() != null){
				Calendar birthdateMax = Calendar.getInstance();
				birthdateMax.setTime(new Date());
				birthdateMax.add(Calendar.YEAR, -modelCommand.getAgeStart());
				filters.put(Filter.BIRTHDATE_MAX, birthdateMax.getTime());
			}
			if (modelCommand.getStart()!=null)
				models = this.modelService.retrieve(filters,modelCommand.getSort(),modelCommand.getDir(),modelCommand.getStart(),modelCommand.getLimit());
			else
				models = this.modelService.retrieve(filters,modelCommand.getSort(),modelCommand.getDir());
			total = this.modelService.retrieveCount(filters);
		
		}
		
		Map<String,Object> _result = new HashMap<String,Object>(4);
		_result.put("data", models);
		_result.put("total", total);
		_result.put("success", true);
		_result.put("message", "OK");
		return _result;
	}
	
	public Map<String,Object> postHandler(HttpServletRequest request, HttpServletResponse response, ModelCommand command){
		
		String firstName = null;
		String lastName = null;
		Date birthDate = null;
		Integer idImage = null;
		Boolean gender = null;
		Integer idModelSchool = null;
		String nameModelSchool = null;
		Integer turn = null;
		
		firstName = command.getFirstName();
		lastName = command.getLastName();
		birthDate = command.getBirthDate();
		idImage = command.getIdImage();
		gender = command.getGender();
		idModelSchool = command.getIdModelSchool();
		turn = command.getTurn();
		nameModelSchool = command.getNameModelSchool();
		
		Model model = this.modelService.create();
		if (firstName!=null)
			model.setFirstName(firstName);
		if (lastName!=null)
			model.setLastName(lastName);
		if (birthDate!=null)
			model.setBirthDate(birthDate);
		if (idImage!=null)
			model.setImage(this.imageService.retrieve(idImage)); 
		if (gender!=null)
			model.setGender(gender);
		if (idModelSchool!=null)
			model.setModelschool(this.modelService.modelSchoolRetrieve(idModelSchool));
		else if (nameModelSchool!=null&&!nameModelSchool.isEmpty()){
			ModelSchool school = new ModelSchool();
			school.setName(nameModelSchool);
			model.setModelschool(school);
		}

		model.setTurn(turn);	
		model = this.modelService.update(model);
		
		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("data", model);
		_result.put("success", model!=null);
		return _result;
	}
	
	public Map<String,Object> putHandler(HttpServletRequest request, HttpServletResponse response, ModelCommand command){
		
		Integer id = null;
		String firstName = null;
		String lastName = null;
		Date birthDate = null;
		Integer idImage = null;
		Boolean gender = null;
		Integer idModelSchool = null;
		String nameModelSchool = null;
		Integer turn = null;
		
		id = command.getId();
		firstName = command.getFirstName();
		lastName = command.getLastName();
		birthDate = command.getBirthDate();
		idImage = command.getIdImage();
		gender = command.getGender();
		idModelSchool = command.getIdModelSchool();
		turn = command.getTurn();
		nameModelSchool = command.getNameModelSchool();
		
		Model model = this.modelService.retrieve(id);
		if (model!=null){
			if (firstName!=null)
				model.setFirstName(firstName);
			if (lastName!=null)
				model.setLastName(lastName);
			if (birthDate!=null)
				model.setBirthDate(birthDate);
			if (idImage!=null)
				model.setImage(this.imageService.retrieve(idImage)); 
			if (gender!=null)
				model.setGender(gender);
			if (idModelSchool!=null)
				model.setModelschool(this.modelService.modelSchoolRetrieve(idModelSchool));
			else if (nameModelSchool!=null){
				ModelSchool school = new ModelSchool();
				school.setName(nameModelSchool);
				model.setModelschool(school);
			}
			model.setTurn(turn);
			model = this.modelService.update(model);
		}
		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("data", model);
		_result.put("success", model!=null);
		return _result;
	}
	
	public Map<String,Object> tagHandler(HttpServletRequest request, HttpServletResponse response, ModelCommand command){
		Integer id = command.getId();
		Integer[] idsImage = command.getIdsImage();
		boolean _success = false;
		
		try{
			Model model = this.modelService.retrieve(id);
			List<Image> images = new ArrayList<Image>();
			for (Integer idImage : idsImage) {
				Image image = this.imageService.retrieve(idImage);
				if (!image.isTagged()){
					image.setTagged(true);
					this.imageService.update(image);
				}
				images.add(image);
			}
			model.getImages().addAll(images);
			this.modelService.update(model);
			_success = true;
		}catch(Exception e){e.printStackTrace();}
		
		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("success", _success);
		return _result;
	}
	
	public Map<String,Object> untagHandler(HttpServletRequest request, HttpServletResponse response, ModelCommand command){
		Integer id = command.getId();
		Integer[] idsImage = command.getIdsImage();
		boolean _success = false;
		
		try{
			Model model = this.modelService.retrieve(id);
			List<Image> images = new ArrayList<Image>();
			for (Integer idImage : idsImage) {
				Image image = this.imageService.retrieve(idImage);
				if (image.isTagged()){
					image.setTagged(false);
					this.imageService.update(image);
				}
				images.add(image);
			}
			model.getImages().removeAll(images);
			this.modelService.update(model);
			_success = true;
		}catch(Exception e){e.printStackTrace();}
		
		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("success", _success);
		return _result;
	}
	
	public Map<String,Object> deleteHandler(HttpServletRequest request, HttpServletResponse response, ModelAddressCommand command){

		Integer id = command.getId();
		
		boolean _success = this.modelService.delete(id);
		
		Map<String,Object> _result = new HashMap<String,Object>(1);
		_result.put("success", _success);
		return _result;
	}
}
